/* *****************************************************************
 *
 * Download latest Blinker library here:
 * https://github.com/blinker-iot/blinker-library/archive/master.zip
 * 
 * 
 * Blinker is a cross-hardware, cross-platform solution for the IoT. 
 * It provides APP, device and server support, 
 * and uses public cloud services for data transmission and storage.
 * It can be used in smart home, data monitoring and other fields 
 * to help users build Internet of Things projects better and faster.
 * 
 * Make sure installed 2.7.4 or later ESP8266/Arduino package,
 * if use ESP8266 with Blinker.
 * https://github.com/esp8266/Arduino/releases
 * 
 * Make sure installed 1.0.5 or later ESP32/Arduino package,
 * if use ESP32 with Blinker.
 * https://github.com/espressif/arduino-esp32/releases
 * 
 * Docs: https://diandeng.tech/doc
 *       
 * 
 * *****************************************************************
 * 
 * Blinker 库下载地址:
 * https://github.com/blinker-iot/blinker-library/archive/master.zip
 * 
 * Blinker 是一套跨硬件、跨平台的物联网解决方案，提供APP端、设备端、
 * 服务器端支持，使用公有云服务进行数据传输存储。可用于智能家居、
 * 数据监测等领域，可以帮助用户更好更快地搭建物联网项目。
 * 
 * 如果使用 ESP8266 接入 Blinker,
 * 请确保安装了 2.7.4 或更新的 ESP8266/Arduino 支持包。
 * https://github.com/esp8266/Arduino/releases
 * 
 * 如果使用 ESP32 接入 Blinker,
 * 请确保安装了 1.0.5 或更新的 ESP32/Arduino 支持包。
 * https://github.com/espressif/arduino-esp32/releases
 * 
 * 文档: https://diandeng.tech/doc
 *       
 * 
 * *****************************************************************/

#define BLINKER_WIFI
#define BLINKER_MIOT_LIGHT

#include <Blinker.h>
#include <FastLED.h>

char auth[] = "06b28903adf0";
char ssid[] = "TopSail";
char pswd[] = "topsail666";

// 新建组件对象
BlinkerButton Button1("btn-abc");
BlinkerNumber Number1("num-abc");

bool ledState;

int counter = 0;

#define LED_B 16
#define LED_COUNT 1 // WS2812B灯珠数量

CRGB leds[LED_COUNT];
// 全局变量
int colorW = 0; 
int global_color = 0;
int colorR = 0;
int colorG = 0;
int colorB = 0;
// 按下按键即会执行该函数
void button1_callback(const String & state)
{
    BLINKER_LOG("get button state: ", state);
    // digitalWrite(LED_B, !digitalRead(LED_B));
    if (ledState == true)
    {
        ledState = false;
        colorW = 0;
        FastLED.setBrightness(colorW);
        leds[0] = CHSV(0,0,0);
        FastLED.show();
    }else
    {
        ledState = true;
        colorW = 50;
        FastLED.setBrightness(colorW);
        leds[0] = CRGB(255,255,255);
        FastLED.show();
    }
    
    
}

// 如果未绑定的组件被触发，则会执行其中内容
void dataRead(const String & data)
{
    BLINKER_LOG("Blinker readString: ", data);
    counter++;
    Number1.print(counter);
}

void miotPowerState(const String & state)
{
    BLINKER_LOG("need set power state: ", state);

    if (state == BLINKER_CMD_ON) {
        // digitalWrite(LED_B, HIGH);
        colorW = 50;
        FastLED.setBrightness(colorW); // 设置亮度为50
        leds[0] = CRGB(255,255,255);
        // leds[0] = color::white;
        FastLED.show();

        BlinkerMIOT.powerState("on");
        BlinkerMIOT.print();
        ledState = true;
    }
    else if (state == BLINKER_CMD_OFF) {
        // digitalWrite(LED_B, LOW);
        colorW = 0;
        FastLED.setBrightness(colorW); // 设置亮度为50
        // 色调，饱和度，亮度
        leds[0] = CHSV(0,0,0);
        FastLED.show();

        BlinkerMIOT.powerState("off");
        BlinkerMIOT.print();
        ledState = false;
    }
}

void miotBright(const String & bright)
{
    BLINKER_LOG("need set brightness: ", bright);

    colorW = bright.toInt();
    FastLED.setBrightness(colorW); // 设置亮度为50
    // leds[0] = CHSV(0,0,colorW);
    leds[0] = CRGB::White;
    FastLED.show();

    BLINKER_LOG("now set brightness: ", colorW);
    
    BlinkerMIOT.brightness(colorW);
    BlinkerMIOT.print();
}

void miotColor(int32_t color)
{
    BLINKER_LOG("need set color: ", color);

    global_color = color;
    colorR = color >> 16 & 0xFF;
    colorG = color >>  8 & 0xFF;
    colorB = color       & 0xFF;

    BLINKER_LOG("colorR: ", colorR, ", colorG: ", colorG, ", colorB: ", colorB);
    FastLED.setBrightness(colorW); // 设置亮度
    leds[0] = CRGB(colorR,colorG,colorB);
    FastLED.show();

    BlinkerMIOT.color(global_color);
    BlinkerMIOT.print();
}

void miotQuery(int32_t queryCode)
{
    BLINKER_LOG("MIOT Query codes: ", queryCode);

    switch (queryCode)
    {
        case BLINKER_CMD_QUERY_POWERSTATE_NUMBER :
            BLINKER_LOG("MIOT Query Power State");
            BlinkerMIOT.powerState(ledState ? "on" : "off");
            BlinkerMIOT.print();
            break;
        case BLINKER_CMD_QUERY_BRIGHTNESS_NUMBER :
            BLINKER_LOG("MIOT Query Brightness");
            BlinkerMIOT.brightness(colorW);
            BlinkerMIOT.print();
            break;
        case BLINKER_CMD_QUERY_COLOR_NUMBER :
            BLINKER_LOG("MIOT Query Color");
            BlinkerMIOT.color(global_color);
            BlinkerMIOT.print();
            break;
        default :
            BlinkerMIOT.powerState(ledState ? "on" : "off");
            BlinkerMIOT.brightness(colorW);
            BlinkerMIOT.color(global_color);
            BlinkerMIOT.print();
            break;
    }
}


void setup()
{
    // 初始化串口
    Serial.begin(115200);
    BLINKER_DEBUG.stream(Serial);
    BLINKER_DEBUG.debugAll();
    
    // 初始化有LED的IO
    FastLED.addLeds<WS2812, LED_B, GRB>(leds, LED_COUNT);
    FastLED.clear();
    FastLED.show();
    FastLED.setBrightness(10); // 设置亮度为50
    ledState = false;

    // 初始化blinker
    Blinker.begin(auth, ssid, pswd);
    Blinker.attachData(dataRead);

    Button1.attach(button1_callback);
    BlinkerMIOT.attachPowerState(miotPowerState);
    BlinkerMIOT.attachQuery(miotQuery);
    BlinkerMIOT.attachBrightness(miotBright);
    BlinkerMIOT.attachColor(miotColor);
}

void loop() {
    Blinker.run();
}
